/*	
 *  Copyright (c) 2009-@year@. The GUITAR group at the University of Maryland. Names of owners of this group may
 *  be obtained by sending an e-mail to atif@cs.umd.edu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 *  documentation files (the "Software"), to deal in the Software without restriction, including without 
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 *	the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
 *	conditions:
 * 
 *	The above copyright notice and this permission notice shall be included in all copies or substantial 
 *	portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 *	LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
 *	EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 *	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 *	THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */
package edu.umd.cs.emut.executor;

import java.util.ArrayList;
import java.util.List;

import edu.umd.cs.emut.exception.EException;
import edu.umd.cs.emut.executor.criteria.IdentificationCriteria;
import edu.umd.cs.emut.executor.matching.DataMatchingCriteria;
import edu.umd.cs.emut.model.EApplication;
import edu.umd.cs.emut.model.EComponent;
import edu.umd.cs.emut.model.EComponentSet;
import edu.umd.cs.emut.model.xml.ComponentType;

/**
 * 
 * Main tool class for all executors
 * 
 * @author <a href="mailto:baonn@cs.umd.edu"> Bao N. Nguyen </a>
 * 
 */
public class Executor {
	List<EExecutionMonitor> lExecutorMonitor = new ArrayList<EExecutionMonitor>();
	EExecutionStrategy strategy;
	EApplication application;

	/**
	 * @return the strategy
	 */
	public EExecutionStrategy getStrategy() {
		return strategy;
	}

	/**
	 * @param strategy the strategy to set
	 */
	public void setStrategy(EExecutionStrategy strategy) {
		this.strategy = strategy;
		strategy.setExecutor(this);
	}

	/**
	 * 
	 */
	public Executor() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the application
	 */
	public EApplication getApplication() {
		return application;
	}

	/**
	 * @param application
	 *            the application to set
	 */
	public void setApplication(EApplication application) {
		this.application = application;
	}

	public void addMonitor(EExecutionMonitor em) {
		this.lExecutorMonitor.add(em);
		em.setExecutor(this);
	}

	public void removeMonitor(EExecutionMonitor em) {
		this.lExecutorMonitor.remove(em);
		em.executor = null;
	}

	/**
	 * Execute the tool
	 * 
	 * @throws EException
	 */
	public final void execute() throws EException {
		application.connect();
		for (EExecutionMonitor em : lExecutorMonitor) {
			em.beforeAllEvents();
		}

		while (strategy.hasNextEvent()) {
			for (EExecutionMonitor em : lExecutorMonitor) {
				em.beforeEvent();
			}

			strategy.performNextEvent();

			for (EExecutionMonitor em : lExecutorMonitor) {
				em.afterEvent();

			}

		}

		for (EExecutionMonitor em : lExecutorMonitor) {
			em.afterAllEvents();
		}

		application.disconnect();

	}

	/**
	 * @return
	 * @see edu.umd.cs.emut.model.EApplication#getAllComponents()
	 */
	public EComponentSet getAllComponents() {
		return application.getAllComponents();
	}

	/**
	 * @return
	 * @see edu.umd.cs.emut.model.EApplication#getAllEventComponents()
	 */
	public EComponentSet getAllEventComponents() {
		
		return application.getAllEventComponents();
	}

	/**
	 * search for a component on the current GUI with the provided searching
	 * criteria
	 * 
	 * <p>
	 * 
	 * @param criteria
	 *            searching criteria
	 * @return component to search if there is and null if there is not
	 * @see IdentificationCriteria
	 */
	public EComponent searchComponent(IdentificationCriteria criteria) {
		EComponentSet allComponents = getAllComponents();
		for (EComponent component : allComponents) {
			if (criteria.isMatched(component))
				return component;
		}
		return null;
	}

	/**
	 * search for a component on the current GUI with the provided searching
	 * criteria and the off-line component data
	 * 
	 * @param dataComponent
	 *            the the off-line component data
	 * @param criteria
	 *            searching criteria
	 * @return
	 */
	public EComponent searchComponent(ComponentType dataComponent,
			DataMatchingCriteria criteria) {
		EComponentSet allComponents = getAllComponents();
		for (EComponent component : allComponents) {
			if (criteria.isMatched(component))
				return component;
		}
		return null;
	}
}
